package com.pms.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class TemplateServlet extends HttpServlet {

	private static final String TRACE_ID = "TEMPLATESERVICE";

	private Hashtable htPageDecorating;

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			//RequestValueObject requestValueObject =(RequestValueObject)request.getAttribute("requestValueObject");
			HttpSession session = request.getSession();
			 
			RequestDispatcher rd = null;
			PageDecorator pageDecorator = null;
			// block for getting agent from agent token class.
			response.setContentType("text/html");
			if(request.getAttribute("plainPage")!=null) {
				String jspPathName = (String) request.getAttribute("nextPage");
				if (jspPathName == null || jspPathName.equals("")) {
					Hashtable errors = (Hashtable) request.getAttribute("error");
					if (errors == null) {
						errors = new Hashtable();
					}
					errors.put("No Forward Tag", "No forward Tag set for PageId:="
							+ JSPUtility.GetInt(request, "pageId", 0) + " / EventId:="
							+ JSPUtility.GetInt(request, "eventId", 0));
					request.setAttribute("error", errors);
					jspPathName = "/jsp/Error.jsp";
				}
				rd = request.getRequestDispatcher(jspPathName);
				rd.include(request, response);
				return;
			}
			
		 

			// if groupid is same as travis groupId in property file then the
			// user is from trvis group
			
			 
		   pageDecorator = (PageDecorator) htPageDecorating.get("user");//for net user
			 

			String jspPathName = (String) request.getAttribute("nextPage");
			if (jspPathName == null || jspPathName.equals("")) {
				Hashtable errors = (Hashtable) request.getAttribute("error");
				if (errors == null) {
					errors = new Hashtable();
				}
				errors.put("No Forward Tag", "No forward Tag set for PageId:="
						+ JSPUtility.GetInt(request, "pageId", 0) + " / EventId:="
						+ JSPUtility.GetInt(request, "eventId", 0));
				request.setAttribute("error", errors);
				jspPathName = "/jsp/Error.jsp";
			}
			 
			if (pageDecorator != null) {
				//System.out.println("@@@@@@@@@@@@@@@@==> i am here inside Page decorator ");
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();
				//header as per groupid 
				//System.out.println("@@@@@@@@@@@@@@@@==> i am here pageDecorator.getHeaderJsp() "+pageDecorator.getHeaderJsp());
				rd = request.getRequestDispatcher(pageDecorator.getHeaderJsp());
				rd.include(request, response);
				//out.println("<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td valign='top' class='leftcolbg'>");				
				//Navigator as per groupid 
				//System.out.println("@@@@@@@@@@@@@@@@==> i am here pageDecorator.getNavigationJsp() "+pageDecorator.getNavigationJsp());
				if(pageDecorator.getNavigationJsp()!=null){
					rd = request.getRequestDispatcher(pageDecorator.getNavigationJsp());
					rd.include(request, response);
				}
				
				//jsp pathname given by the screen flow manager
				rd = request.getRequestDispatcher(jspPathName);
				rd.include(request, response);
				rd = request.getRequestDispatcher(pageDecorator.getFooterJsp());
				rd.include(request, response);
				out.flush();
				out.close();
				//System.out.println(pageDecorator.getHeaderJsp());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {

		ServletContext application = getServletContext();
		String resourcePath = application.getRealPath("/");
		resourcePath += "/WEB-INF/PageDecorator.xml";
		PageDecoratorXmlLoader pageDecoratorXmlLoader = PageDecoratorXmlLoader
				.getInstance();
		htPageDecorating = pageDecoratorXmlLoader.loadSettings(resourcePath);
		// application.setAttribute("actionMapping",htActionMapping);
	}

}
